package com.yao.mall.config;


import com.yao.mall.common.ErrorCode;
import com.yao.mall.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author jiahua.yao
 * @version 1.0
 * @description 全局异常捕获
 * @date 2024/2/29 14:50
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public Result<Object> businessException(BusinessException e){
        log.error("businessException:",e.getMessage(),e);
        return Result.fail(e.getCode(), e.getMessage(), e.getDescription());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result<Object> runtimeException(BusinessException e){
        log.error("runtimeException:",e);
        return Result.fail(ErrorCode.SYSTEM_ERROR, e.getMessage(), "");
    }

}
